package com.xinya.wanandroidclient.ui.web

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.OnBackPressedCallback
import androidx.databinding.ViewDataBinding
import com.xinya.wanandroidclient.R
import com.xinya.wanandroidclient.base.BaseFragment
import com.xinya.wanandroidclient.common.setNoRepeatClick
import com.xinya.wanandroidclient.databinding.FragmentWebBinding
import com.xinya.wanandroidclient.view.NavHostFragment

/**
 *
 * @ClassName:      WebFragment$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/3/11$ 16:52$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class WebFragment : BaseFragment() {
    companion object {
        private const val TAG = "WebFragment"
    }

    private val articleUrl by lazy {
        arguments?.getString("url", "") ?: ""
    }

    private val articleId by lazy {
        arguments?.getInt("id", 0) ?: 0
    }

    private val articleTitle by lazy {
        arguments?.getString("title", "") ?: ""
    }

    override fun getLayoutId() = R.layout.fragment_web

    @SuppressLint("SetJavaScriptEnabled")
    override fun init(savedInstanceState: Bundle?) {
        if (TextUtils.isEmpty(articleUrl)) {
            return
        }

        val webTitleView = dataBinding.webTitle
        val webContentView = dataBinding.webContent
        val webProgressView = dataBinding.webProgress

        requireActivity().onBackPressedDispatcher.addCallback(this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    // 返回
                    if (webContentView.canGoBack()) {
                        webContentView.goBack()
                        return
                    }
                    NavHostFragment.findNavController(this@WebFragment).navigateUp()
                }
            })

        webTitleView.text = articleTitle
        webContentView.apply {
            settings.apply {
                // 使能javaScript
                javaScriptEnabled = true
                layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
                loadWithOverviewMode = true
            }
            webViewClient = object : WebViewClient() {

            }
            webChromeClient = object : WebChromeClient() {
                override fun onProgressChanged(view: WebView?, newProgress: Int) {
                    super.onProgressChanged(view, newProgress)
                    webProgressView.apply {
                        progress = newProgress
                        visibility = if (newProgress == 100) View.INVISIBLE else View.VISIBLE
                    }
                }
            }
        }.loadUrl(articleUrl)
    }

    private lateinit var dataBinding: FragmentWebBinding
    override fun initDataBinding(binding: ViewDataBinding?) {
        dataBinding = binding as FragmentWebBinding
    }

    override fun onClick() {
        dataBinding.apply {
            val webContentView = dataBinding.webContent
            webBack.setNoRepeatClick {
                // 返回
                NavHostFragment.findNavController(this@WebFragment).navigateUp()
//                NavHostFragment.findNavController(this@WebFragment).navigate(R.id.action_web_to_main)
            }
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: ")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause: ")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume: ")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy: ")
    }
}